python在jupyter notebook/terminal/console下的输出清空 您所在的位置:网站首页 python 输出命令 python在jupyter notebook/terminal/console下的输出清空

python在jupyter notebook/terminal/console下的输出清空

2024-07-09 07:42| 来源: 网络整理| 查看: 265

前言:

在用python编程的时候,你是否偶尔有个需求?需要把输出的日志 or 信息清空?以下笔者将从3个角度讨论清空输出的简单方式

1. Jupyter notebook下的输出清空

主要通过IPython.display.clear_output来清空

from IPython.display import clear_output as clear print('before') clear() # 清除输出 print('after')

具体案例 在这里插入图片描述

2. Terminal/Console下的输出清空

使用os.system('cls')或os.system('clear')来清空

import os print('before') os.system('cls' if os.name == 'nt' else 'clear') print('after') 3. 综合 import os, sys def clear_output(): os.system('cls' if os.name == 'nt' else 'clear') if 'ipykernel' in sys.modules: from IPython.display import clear_output as clear clear() print('before') clear_output() # 清除输出 print('after')


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有